<template>
    <div class="detail">
      <transition
              @before-enter="beforeEnter"
              @enter="enter"
              @after-enter="afterEnter">
              <div class="ball" v-show="flag" ref="ball"></div>
            </transition>
        <!-- 轮播图区域 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                        <mt-swipe :auto="4000">
                        <mt-swipe-item v-for="(item,index) in lunbotuList" :key="index">
                            <img :src="item" alt="">
                        </mt-swipe-item>
                        </mt-swipe>
                </div>
			      </div>
		    </div>
    <!-- 购买区域 -->
        <div class="mui-card">
            <div class="mui-card-header">{{infoList.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价:<del class="old">￥{{infoList.old}}</del>&nbsp;&nbsp;销售价:<span class="now">￥{{infoList.price}}</span>
                    </p>
                    <div class="mid">购买数量:
                        <div class="mui-numbox" data-numbox-min='1' data-numbox-max='9'>
                            <button class="mui-btn mui-btn-numbox-minus" type="button" @click="sub">-</button>
                            <input id="test" class="mui-input-numbox" type="number" :value="counter" ref="test"/>
                            <button class="mui-btn mui-btn-numbox-plus" type="button" @click="add">+</button>
                        </div>
                    </div>
                    <p class="btns">
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addToCart">加入购物车</mt-button>
                    </p>
                </div>
            </div>
		</div>
    <!-- 介绍区域 -->
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号:1c154cacaf00</p>
                    <p>库存:99+</p>
                    <p>上架时间:2019-04-09</p>
                </div>
            </div>
		</div>
    </div>
</template>

<script>
// import mui from '../../lib/mui/js/mui.js';
export default {
data() {
    return {
      lunbotuList: [], // 保存轮播图的数组
      id:this.$route.query.id,
      infoList:[],
      flag:false,
      counter:1
    };
  },
  created() {
    this.getLunbotu();
    this.getInfo();
  },
  // mounted(){
  //   mui(".mui-numbox").numbox()
  // },
  methods: {
    getLunbotu(){
      this.axios(
        "http://10.31.162.198/1810-3/Vue/vue-cms/src/lib/PHP/goodsinfo.php?id="+this.id,
        {id:this.id}
      ).then(res=>{
        // console.log(res.data);
        this.lunbotuList = JSON.parse(res.data.pic1);
        //  console.log(this.lunbotuList)
      })
    },
    getInfo(){
      this.axios(
        "http://10.31.162.198/1810-3/Vue/vue-cms/src/lib/PHP/goodsinfo.php?id="+this.id,
        {id:this.id}
    ).then(res=>{
        this.infoList = res.data;
        //  console.log(this.infoList)
      })
    },
    addToCart(){
      //添加购物车
      this.flag = !this.flag;
      var goodsinfo = {id:this.id,count:this.counter,price:this.infoList.price,selected:true};
      // console.log(goodsinfo)
      //调用仓库中的mutations将商品加入购物车
      this.$store.commit("addToCar",goodsinfo);
      console.log(this.$store.state)
    },
    beforeEnter(el){
      el.style.transform = "translate(0,0)"
    },
    enter(el,done){
      el.offsetWidth;
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      const badgePosition = document.getElementById("badge").getBoundingClientRect();
      const xDistance = badgePosition.left - ballPosition.left;
      const yDistance = badgePosition.top - ballPosition.top;
      el.style.transform = `translate(${xDistance}px,${yDistance}px)`;
      el.style.transition = "all 1s";
      done();
    },
    afterEnter(el){
      this.flag = !this.flag
    },
    // counterChange(){
    //   console.log(this.$refs.test.value)
    // },
    add(){
      this.counter++;
    },
    sub(){
      this.counter--;
    }
  }
}
</script>

<style lang="scss" scoped>
.detail{
    background: #eee;
    overflow: hidden;
    .mint-swipe {
        height: 15rem;
        text-align: center;
        img {
        width: 80%;
        height: 100%;
    }
  }
  .mid{
      margin: 10px 0;
  }
  .price{
        .now{
        color: #f00;
        font-size: 14px;
        font-weight: bold;
        }
    }
    .btns{
        display: flex;
        justify-content: space-between;
    }
      .ball{
        width: 15px;
        height: 15px;
        background: #f00;
        border-radius: 50%;
        left: 141px;
        top: 427px;
        position: absolute;
        z-index: 999;
      }

}
</style>
